package com.xxx.base.rest.response;

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

import java.io.Serializable;

/**
 * rest 请求返回的基本数据结构
 *
 * @author hubert
 */
public class RestRet<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    public static final RestRet OK = new RestRet().setSuccess(true);

    private boolean success;
    private T       data;
    private String  errorMsg;
    /**
     * 错误id(方便查找对应的错误位置)
     */
    private String  errorId;


    public static <T> RestRet ok(T data) {
        return new RestRet().setSuccess(true).setData(data);
    }


    public static <T> RestRet fail(String errMsg) {
        return new RestRet().setErrorMsg(errMsg);
    }


    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this, ToStringStyle.NO_CLASS_NAME_STYLE);
    }


    /**
     * result status.
     *
     * @return true if success.
     */
    public boolean isSuccess() {
        return success;
    }


    /**
     * result data, general is a dto.
     *
     * @return
     */
    public T getData() {
        return data;
    }


    public RestRet setSuccess(boolean pSuccess) {
        success = pSuccess;
        return this;
    }


    public RestRet setData(T data) {
        this.data = data;
        return this;
    }


    public String getErrorMsg() {
        return errorMsg;
    }


    public RestRet<T> setErrorMsg(String errorMsg) {
        this.errorMsg = errorMsg;
        return this;
    }


    public String getErrorId() {
        return errorId;
    }


    public RestRet<T> setErrorId(String errorId) {
        this.errorId = errorId;
        return this;
    }
}
